Time Series

Time Series

Chapter 11

Lines for time series, w/ time on the x-axis

Fix legend

2010 only

Overall long-term trend (secular)

Smoother (non-parametric)

Default smoothing parameter .75

Change smoothing parameter .1

Change smoothing parameter .5

Change smoothing parameter .9

How many data values between the peaks?

Mark the pattern

Facet to test (remove) cyclical pattern

Another view

With smoother

Air Passengers

plot(AirPassengers)

monthplot()

monthplot(AirPassengers)

What about the abnormalities?

Different pattern for Christmas Week

Highlight the abnormality

Comparisons (multi-line plots)

Scale the data (create an index)

Each value is divided by the first value for that group and multiplied by 100:

time key value
1 y1 9.025610
2 y1 10.747935
3 y1 11.027932
4 y1 8.177154
5 y1 13.061942
6 y1 4.142747
1 y2 81.222975
2 y2 85.541731
3 y2 128.257508
4 y2 20.148328
5 y2 116.806389
6 y2 164.865557
time key value index
1 y1 9.025610 100.00000
2 y1 10.747935 119.08265
3 y1 11.027932 122.18490
4 y1 8.177154 90.59947
5 y1 13.061942 144.72088
6 y1 4.142747 45.89992
1 y2 81.222975 100.00000
2 y2 85.541731 105.31716
3 y2 128.257508 157.90792
4 y2 20.148328 24.80619
5 y2 116.806389 143.80954
6 y2 164.865557 202.97897

Discrete data

Better for individual values

Gaps

Add points to show the frequency of the data

Another option: leave gaps

Coding

Use data class

str(dfman)
## Classes 'tbl_df', 'tbl' and 'data.frame':    147 obs. of  3 variables:
##  $ Day  : chr  "Fri" "Sat" "Sun" "Mon" ...
##  $ Date : Date, format: "2016-11-18" "2016-11-19" ...
##  $ Gross: num  72658 90040 93800 32023 29989 ...
##  - attr(*, "spec")=List of 2
##   ..$ cols   :List of 3
##   .. ..$ Day  : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ Date :List of 1
##   .. .. ..$ format: chr ""
##   .. .. ..- attr(*, "class")= chr  "collector_date" "collector"
##   .. ..$ Gross: list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr  "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
ggplot(dfman, aes(Date, Gross)) + geom_line() +
  theme_grey(16)

dfman2016 <- dfman %>% 
  filter(Date <= as.Date("2016-12-31"))
ggplot(dfman2016, aes(Date, Gross)) + geom_line() +
  theme_grey(16)

Try readr (read_csv)

Consider lubridate